package com.example.text.ui.mall.adepter

import android.util.Log
import androidx.databinding.ViewDataBinding
import com.alibaba.android.arouter.launcher.ARouter
import com.bigkoo.convenientbanner.ConvenientBanner
import com.bumptech.glide.Glide
import com.bumptech.glide.load.engine.DiskCacheStrategy
import com.chad.library.adapter.base.BaseMultiItemQuickAdapter
import com.chad.library.adapter.base.module.LoadMoreModule
import com.chad.library.adapter.base.viewholder.BaseDataBindingHolder
import com.example.text.R
import com.example.text.cyclone.Router
import com.example.text.databinding.ItemMallHomeBannerBinding
import com.example.text.databinding.ItemMallHomeGoodsBinding
import com.example.text.databinding.ItemMallHomeRowTitleBinding
import com.example.text.ui.mall.model.MallBanner
import com.example.text.ui.mall.model.MallHomeRowItem
import com.example.text.ui.mall.model.MallRowTags
import com.example.text.ui.mall.model.MallRowTypes
import com.example.text.ui.mall.view.GoodsDetailActivity

import com.example.text.utils.GSON
import com.example.text.utils.ToastUtils
import kotlin.math.log

class MallHomeMultiAdapter(
    data: ArrayList<MallHomeRowItem>,
    private val onClick: ((tag: String) -> Unit)?,
    private val onBookingClick: ((MallBanner) -> Unit)? = null
) :
    BaseMultiItemQuickAdapter<MallHomeRowItem, BaseDataBindingHolder<ViewDataBinding>>(data),
    LoadMoreModule {
    private var hasInitBanner = false
    private var bannerIsTurning = false

    //在 init 块中添加所有可能的 item 类型布局
    init {
        addItemType(MallRowTypes.BANNER, R.layout.item_mall_home_banner)
        addItemType(MallRowTypes.TITLE, R.layout.item_mall_home_row_title)
        addItemType(MallRowTypes.GOODS, R.layout.item_mall_home_goods)
    }

    //convert 方法中根据不同的 itemType 处理不同的视图绑定
    override fun convert(holder: BaseDataBindingHolder<ViewDataBinding>, item: MallHomeRowItem) {
        when (item.itemType) {
            MallRowTypes.BANNER -> {
                val binding = holder.dataBinding as ItemMallHomeBannerBinding
                convertBanner(binding, item)
            }
            MallRowTypes.TITLE -> {
                val binding = holder.dataBinding as ItemMallHomeRowTitleBinding
                convertTitle(binding, item)
            }
            MallRowTypes.GOODS -> {
                val binding = holder.dataBinding as ItemMallHomeGoodsBinding
                convertGoods(binding, item)
            }
        }
        holder.dataBinding?.executePendingBindings()
    }
    private fun convertGoods(binding: ItemMallHomeGoodsBinding, item: MallHomeRowItem) {
        item.goods?.let {
            binding.item = it
            binding.commonPrice.item = it
        }
        binding.ivGoodsPic.let {
            Glide.with(context)
                .load(item.goods?.mainImgUrl)
                .placeholder(R.mipmap.icon_mall_placeholder) // 加载中的占位图
                .error(R.mipmap.icon_mall_placeholder)       // 加载失败的占位图
                .diskCacheStrategy(DiskCacheStrategy.ALL)
                .into(it)
        }
        // 查看商品详情
        binding.root.setOnClickListener {
            GoodsDetailActivity.jump(item.goods?.id ?: 0, false)
            //Log.d("ggg", item.goods?.id.toString()
        }
    }
    private fun convertTitle(binding: ItemMallHomeRowTitleBinding, item: MallHomeRowItem) {
        binding.item = item
        binding.btnJumpMore.setOnClickListener {
            when (item.tag) {
                MallRowTags.ACTIVITY -> {// 更多活动
                   // ActListActivity.jump()
                }
                MallRowTags.GOODS -> {// 更多商品
//                    MallGroupGoodsListActivity.jump(
//                        groupId = item.id,
//                        typeName = item.title ?: "商品列表",
//                        fromHome = true
//                    )
                }
                else -> {
                    onClick?.invoke(item.tag ?: "")
                }
            }
        }
    }


    private fun convertBanner(binding: ItemMallHomeBannerBinding, item: MallHomeRowItem) {
        val banners = item.banners
       // Log.d("aaa", GSON.toJson(banners))
        val bannerCount = banners?.size ?: 0
        if (hasInitBanner) {// 已初始化过Banner
            binding.mallBanner.apply {
                setPages({
                    MallBannerHolder(onBookingClick = {
                        onBookingClick?.invoke(it)
                    })
                }, banners)


                if (bannerCount > 1) {
                    setPageIndicator(
                        intArrayOf(
                            R.drawable.icon_banner_indicator_0,
                            R.drawable.icon_banner_indicator_1
                        )
                    )
                } else {
                    setPageIndicator(intArrayOf(0, 0))
                }
                setPointViewVisible(bannerCount > 1)
                isCanLoop = bannerCount > 1
                isManualPageable = bannerCount >= 1
                if (bannerCount <= 1) {
                    bannerIsTurning = false
                    stopTurning()
                } else if (!bannerIsTurning) {
                    bannerIsTurning = true
                    binding.mallBanner.startTurning(3_000)
                }
                notifyDataSetChanged()
            }
        } else {// 未初始化过Banner
            hasInitBanner = true
            (binding.mallBanner as ConvenientBanner<MallBanner>).apply {
                if (bannerCount > 1) {
                    setPageIndicator(
                        intArrayOf(
                            R.drawable.icon_banner_indicator_0,
                            R.drawable.icon_banner_indicator_1
                        )
                    )
                } else {
                    setPageIndicator(intArrayOf(0, 0))
                }
                setPointViewVisible(bannerCount > 1)
                isCanLoop = bannerCount > 1
                setPages({
                    MallBannerHolder(onBookingClick = {
                        onBookingClick?.invoke(it)
                    })
                }, banners)
                isManualPageable = bannerCount >= 1
            }
            if (bannerCount > 1) {
                bannerIsTurning = true
                binding.mallBanner.startTurning(3_000)
            }
        }
    }
}